
#include "motor_dri.h"
#include "stm32f0xx_ll_gpio.h"
#include "stm32f0xx_ll_tim.h"
#include "common.h"
#include "stm32f0xx_ll_usart.h"
#include "orbital.h"
#include "oled.h"
#include "GPIO.h"
#include "KEYSCAN.h"

PARA_STRUCT glob_para;
ID_CODE wifi_id;

uint8_t m_start = 0;
uint8_t ab_set_tag = 0;
uint8_t slow_time = 0;
uint8_t p_upORdown=0;
uint8_t slowly_now=0;
uint8_t stop_1S=0;

uint32_t boot_time=0;
uint16_t led_time=0;

uint32_t tatal_pulse = 0;
uint32_t frames_time = 0;
uint32_t expouse_time = 0;
uint32_t move_distance =0;
uint32_t slow_max =0;
uint32_t slow_miix=0;

extern uint32_t fast_time_bakeup;
extern uint32_t stop_time_bakeup ;
extern uint32_t p_move_time ;
extern uint32_t move_time_bakeup;
extern uint8_t move_begin;
extern uint8_t move_begin_backup;
extern uint8_t dynamic_speed;

/*const uint16_t speed_table[]={
	969,951,933,915,897,879,861,843,825,807,	// 18
	789,773,757,741,725,709,693,677,661,645,	// 16
	629,615,601,587,573,559,545,531,517,501,	// 14
	485,471,457,443,429,415,401,387,359,347,	// 12
	335,325,315,305,295,285,275,265,245,235,	// 10
	225,217,209,201,193,185,177,169,161,153,	// 8
	145,139,133,127,121,115,109,103,97,91,		// 6
	85,81,77,73,69,65,61,57,53,49,				// 4
	45,43,41,39,37,35,33,31,29,27,				// 2
	25,24,23,22,21,20,19,18,17,16				// 1
};

const uint16_t speed_table[]={
	2095,2010,1944,1883,1827,1776,1730,1689,1653,1622,	// 18
	1596,1574,1554,1533,1521,1491,1470,1449,1428,1407,	// 16
	1386,1365,1344,1323,1302,1281,1260,1239,1218,1197,	// 14
	1176,1155,1134,1113,1092,1071,1050,1029,1008,987,	// 12
	966,945,924,903,882,861,840,819,798,777,	// 10
	756,735,714,693,672,651,630,609,588,567,	// 8
	546,525,504,483,462,441,420,399,378,357,		// 6
	336,315,294,273,252,231,210,189,168,226,				// 4
	206,187,168,152,136,121,107,94,82,71,				// 2
	//61,52,44,37,31,26,22,19,17,16				// 1
	19,18,17,16,15,14,13,12,11,10
};*/

//uint32_t get_tick_time(void)
//{
//	return tick;
//}

//void tick_count(void)
//{
//	tick++;
//}

void set_boot_time(uint16_t timep)
{
	boot_time = timep;
}

void set_1S(uint8_t stop_timep)
{
	stop_1S = stop_timep;
	set_boot_time(1000);
}
void gpio_pulldown_int(GPIO_TypeDef *GPIOx, uint32_t PinMask)
{
	LL_GPIO_InitTypeDef GPIO_InitStruct;
	
	GPIO_InitStruct.Pin = PinMask;
	GPIO_InitStruct.Mode = LL_GPIO_MODE_OUTPUT;
	GPIO_InitStruct.Speed = LL_GPIO_SPEED_FREQ_LOW;
	GPIO_InitStruct.OutputType = LL_GPIO_OUTPUT_OPENDRAIN;
	GPIO_InitStruct.Pull = LL_GPIO_PULL_NO;
	LL_GPIO_Init(GPIOx, &GPIO_InitStruct);
}

void time_count(void)
{
	if(boot_time)boot_time--;
	if(slow_time)slow_time--;
	if(led_time)led_time--;
	if(move_begin>1)p_move_time++;
	if(get_init_time)get_init_time--;
//	delay_The_countdown();
	//tick_count();
}

void time_delay_ms(uint16_t counts)
{
	boot_time = counts;
	while(boot_time)
	{
		Feed_IWDG();
	}
}

//globle para
void para_init(void)
{
	glob_para.orbital_dir = B_TO_A;
	glob_para.orbital_speed = 100;
	glob_para.auto_return_tag = 1;
	glob_para.shoot_amount = 168;
	wifi_id.language_sel = ENGLISH;
	wifi_id.sleep_time = 5;
	wifi_id.brightness = 20;
	glob_para.duration_h = 0;
	glob_para.duration_m = 10;
	glob_para.duration_s = 0;
	glob_para.delay_time_h = 0;
	glob_para.delay_time_m = 0;
	glob_para.tatol_time_h = 0;
	glob_para.tatol_time_m = 9;
	glob_para.fram_time_h = 0;
	glob_para.fram_time_m = 0;
	glob_para.fram_time_s = 0;
	glob_para.shut_time_h = 0;
	glob_para.shut_time_m = 0;
	glob_para.shut_time_s = 0;
}

void motor_out_enable(void)
{
	//LL_GPIO_ResetOutputPin(GPIOB, LL_GPIO_PIN_12); //
}
void motor_out_disable(void)
{
	//LL_GPIO_SetOutputPin(GPIOB, LL_GPIO_PIN_12); //
}
void motor_dirver_mode_select(void)
{
	// control stepper motor 
	LL_GPIO_SetOutputPin(GPIOB, LL_GPIO_PIN_2); //stepper motor
//	//control DC motor
//	LL_GPIO_ResetOutputPin(GPIOA, LL_GPIO_PIN_5); //DC motor
}



void motor_speed_set(uint32_t speedd)
{
	LL_TIM_SetPrescaler(TIM3, speedd);
	LL_TIM_GenerateEvent_UPDATE(TIM3);
}

uint32_t speed_calculat(uint8_t p_speed)
{
	uint32_t temp=0;

	//	temp = speed_table[p_speed-1];
	
	/*if(p_speed >= 10)
	{
		temp = 2*(100 - p_speed) + 13;//+(100-p_speed)*2;
	}
	else*/
	{
		temp = 50*(100 - p_speed) + 20;
	}

	return temp;
}

void motor_speed_adjust(uint8_t p_speed)
{
	motor_speed_set(speed_calculat(p_speed));
	
//	LL_TIM_SetPrescaler(TIM3, speed_table[p_speed-1]);
//	LL_TIM_GenerateEvent_UPDATE(TIM3);
}

void motor_start(void)
{
	LL_TIM_EnableIT_UPDATE(TIM3);
	LL_TIM_ClearFlag_UPDATE(TIM3);
	LL_TIM_EnableCounter(TIM3);
	
}

void motor_stop(void)
{
	
	//LL_TIM_ClearFlag_UPDATE(TIM3);
	LL_TIM_DisableCounter(TIM3);
	LL_TIM_DisableIT_UPDATE(TIM3);
	LL_TIM_ClearFlag_UPDATE(TIM3);
	//motor_out_disable();
}

void motor_prescale_setting(uint8_t p_prescal)
{
	switch(p_prescal)
	{
		case PRESCALE_1:
		{
			GPIO_OUT_init(GPIOB, LL_GPIO_PIN_10);
			LL_GPIO_ResetOutputPin(GPIOB, LL_GPIO_PIN_0); //M0
			LL_GPIO_ResetOutputPin(GPIOB, LL_GPIO_PIN_2); //M1
			break;	
		}
		case PRESCALE_2:
		{
			GPIO_OUT_init(GPIOB, LL_GPIO_PIN_10);
			LL_GPIO_SetOutputPin(GPIOB, LL_GPIO_PIN_10); //M0
			LL_GPIO_ResetOutputPin(GPIOB, LL_GPIO_PIN_2); //M1
			break;	
		}
		case PRESCALE_4:
		{
			LL_GPIO_ResetOutputPin(GPIOB, LL_GPIO_PIN_2); //M1
			gpio_pulldown_int(GPIOB, LL_GPIO_PIN_10);
			LL_GPIO_SetOutputPin(GPIOB, LL_GPIO_PIN_10); //M0
			break;	
		}
		case PRESCALE_8:
		{
			GPIO_OUT_init(GPIOB, LL_GPIO_PIN_10);
			LL_GPIO_ResetOutputPin(GPIOB, LL_GPIO_PIN_10); //M0
			LL_GPIO_SetOutputPin(GPIOB, LL_GPIO_PIN_2); //M1
			break;	
		}
		case PRESCALE_16:
		{
			GPIO_OUT_init(GPIOB, LL_GPIO_PIN_10);
			LL_GPIO_SetOutputPin(GPIOB, LL_GPIO_PIN_10); //M0
			LL_GPIO_SetOutputPin(GPIOB, LL_GPIO_PIN_2); //M1
			break;	
		}
		case PRESCALE_32:
		{
			gpio_pulldown_int(GPIOB, LL_GPIO_PIN_10);
			LL_GPIO_SetOutputPin(GPIOB, LL_GPIO_PIN_10); //M0
			LL_GPIO_SetOutputPin(GPIOB, LL_GPIO_PIN_2); //M1
			break;	
		}

		default:
		{
			// PRESCALE_32:
			gpio_pulldown_int(GPIOB, LL_GPIO_PIN_10);
			LL_GPIO_SetOutputPin(GPIOB, LL_GPIO_PIN_10); //M0
			LL_GPIO_SetOutputPin(GPIOB, LL_GPIO_PIN_2); //M1
			break;
		}
	}
}

void wait_for_init(void)
{
	connection_flag = 0;
	if(wifi_id.wakaup_tag == 0)
	{
		if(CHARGE_STATUS == CHARGING)
		{
			LCD_Fill(0, 58, LCD_W-1, 135, BLACK);
			set_power_on_int(POWER_OFF);
			TurnOff_ShutDonw_Battery_dis(1, 0, 1);
		//	TurnOff_ShutDonw_Battery_dis(0, 0, 1);
		}
	}
	
//	cursor_id = 0;
//	page_id = LOCUS_PAGE;
	/// oled_print_screen();
	/// LCD_Fill(0, 140, LCD_W-1, 160, BLACK);

	//page_id = MAIN_ID;
	//change_page();
}

void motor_init(void)
{
	para_read_from_flash();
//	motor_prescale_setting(PRESCALE_32);
//	motor_dirver_mode_select();
//	motor_direction_change();
//	motor_speed_adjust(SLOW_THRESHOLD);
	glob_para.delay_time_h = 0;
	glob_para.delay_time_m = 0;
	glob_para.tatol_time_h = 0;

	delay_mode_init();
	//para_init();
	parameter_init();
	boot_time = 500;
	product_s = 0;
	cursor_id_back = 1;
}


void slider_upORdown(uint8_t p_type)
{
	p_upORdown = p_type;
}


void motor_slowly_startedORstop(void)
{
	if(p_upORdown==0)return;
//	if(page_id == MANUAL_MODE)if(ramp_flag == 0)return;
	if(page_id == AUTO_MODE)if(ramp_flag11 == 0)return;
	if(slow_time)return;
	
	slow_time = 15; //(120-glob_para.orbital_speed) *2+(100-glob_para.orbital_speed)*7;

//	if(glob_para.auto_return_tag)
	{
	//	if(speed_back < tempp)
		{
			if(p_upORdown == SLOW_STOP)
			{
				//p_speed = 3*(100 - glob_para.orbital_speed) + 10;
				motor_speed_set(dynamic_speed);
				
				//LL_TIM_SetPrescaler(TIM3, dynamic_speed);
				//LL_TIM_GenerateEvent_UPDATE(TIM3);
				
				if(dynamic_speed < slow_miix)
				{
					dynamic_speed++;

					if(m_start==0)
					{
						if(dynamic_speed == 101)
						{
							motor_stop();
						}
					}
				}
				else 
				{
					p_upORdown = 0;
					if(m_start==0)motor_stop();
				}
			}
			else if(p_upORdown == SLOW_START)
			{
				motor_speed_set(dynamic_speed);
				
				if(dynamic_speed > slow_max)
				{
					dynamic_speed--;
				}
				else 
				{
					p_upORdown = 0;
				}
			}
		}
//		else 
//		{
//			p_upORdown = 0;
//			set_slowly_now(0);
//		}
	}
	/*else
	{
		if(m_start||delay_start)
		{
			p_upORdown = 0;
			motor_stop();
			mcu_set_data_to_apk(0x0d, 0xff); // stop data send to APP
		}
	}*/
}

// active in single shot
void shutter_adjust(void)
{
	
//	auto_add_speed();
	
	/*if(shutter_tag)
	{
		//TURN_ON_CAMERA;
		if(boot_time)return;
		SHUTTER_OFF;
		shutter_tag = 0;
	}*/
	
}

uint32_t move_time_calculate(void)
{
	float pluse_basic_time=0.45925; // ==(float)( speed_calculat(DELAY_M_SPEED)+1) * (float)501 / (float)48000; //ms unit
	uint32_t move_time_p=0; // 
	uint32_t lenth_pulse = 0;
	uint32_t pix_num=0;
	
	lenth_pulse = glob_para.b_point - glob_para.a_point;
	if(glob_para.shoot_amount)pix_num = glob_para.shoot_amount - 1;
	
	if(lenth_pulse)
	{
		if(pix_num)
		{
			move_time_bakeup = lenth_pulse / pix_num;
			
			move_time_p = (move_time_bakeup * pluse_basic_time);
		}
		else
		{
			move_time_p = 0;
			move_time_bakeup = 0;
		}
	}
	else
	{
		move_time_p = 0;
		move_time_bakeup = 0;
	}
	
	return move_time_p;
}
void set_slowly_now(uint8_t datat)
{
	slowly_now = datat;
}

void auto_return_acstion(void)
{
	if((page_id == AUTO_MODE))
	{
		if(glob_para.auto_return_tag)
		{
			if(ramp_flag11)
			{
				if(delay_speed < 113)slider_upORdown(SLOW_START);		
			}
			
			motor_start();
		}
		else
		{
			set_slowly_now(0);
			motor_stop();
			m_start = 0;
			status_display(4);
		}
	}
	//else if(page_id == MANUAL_MODE)
	//{
	//	motor_stop();
	//	status_display(2);
	//}
}
void auto_return_check(void)
{
	if((m_start==0))return;
	if(check_start_point)return;
	
	if(glob_para.pulse_cout <= glob_para.a_point)
	{
		if(glob_para.orbital_dir==A_TO_B)return;

		/*if(page_id == MANUAL_MODE)
		{
			motor_stop();
			m_start = 0;
			status_display(VINDEX_THREE);
			return;
		}
		else */if(page_id == AUTO_MODE)
		{
			set_1S(0x01);
			//set_boot_time();
			motor_stop();
			return;
		}
		
		glob_para.orbital_dir=A_TO_B;
		motor_direction_change();
		shoot_mode_adjust();
		video_dir_display(VINDEX_FOUR);
		
		//auto_return_acstion();
	}
	else if(glob_para.pulse_cout >= glob_para.b_point)
	{
		if(glob_para.orbital_dir==B_TO_A)return;

		/*if(page_id == MANUAL_MODE)
		{
			motor_stop();
			m_start = 0;
			status_display(VINDEX_THREE);
			return;
		}
		else */if(page_id == AUTO_MODE)
		{
			set_1S(0x02);
			//set_boot_time(1000);
			motor_stop();
			return;
		}
		glob_para.orbital_dir=B_TO_A;
		motor_direction_change();
		shoot_mode_adjust();
		video_dir_display(VINDEX_FOUR);
		
		//auto_return_acstion();
	}
}

// begin slow nearly a point or b point
void slow_spee_check(void)
{
	if(check_start_point)return;
	
	if(m_start==0)return;
	if((page_id != MANUAL_MODE) && (page_id != AUTO_MODE))return;
	//if(page_id == MANUAL_MODE)if(ramp_flag == 0)return;
	if(page_id == AUTO_MODE)if(ramp_flag11 == 0)return;
	if(page_id == MANUAL_MODE)if(glob_para.orbital_speed < SLOW_THRESHOLD)return;
	else if(page_id == AUTO_MODE)if(delay_speed < 113)return;
	
	if(glob_para.pulse_cout <= glob_para.a_point+A_POINT_END)
	{
		if(glob_para.orbital_dir==A_TO_B)return;
		if(slowly_now)return;
		
		set_slowly_now(0xff);
		slider_upORdown(SLOW_STOP);
	}
	else if(glob_para.pulse_cout >= glob_para.b_point-A_POINT_END)
	{
		if(glob_para.orbital_dir==B_TO_A)return;
		if(slowly_now)return;
		
		set_slowly_now(0xff);
		slider_upORdown(SLOW_STOP);
	}
	else 
	{
		set_slowly_now(0);
	}
	
}



////////////////////////////////////////// delay mode ////////////////////////////////////
/*void delay_move_time_compara(void)
{
	
	if(move_begin == 1)
	{
		move_distance++;
		if( move_distance>= move_time_bakeup)
		{
			//print_word_data(p_move_time);
			motor_stop();
			move_begin = 2;
			p_move_time = 0;
			move_distance=0;
		}
	}
}

void delay_mode_main(void)
{
//	uint16_t temp=0;
	
	if(delay_start==0)return;
	if(move_begin==0)return;
	
	if(move_begin == 2)
	{
		if(p_move_time >= 1000)
		{
			SHUTTER_ON;
			p_move_time = 0;
			move_begin = 3;
		}
	}
	else if(move_begin == 3)
	{
		if(p_move_time >= fast_time_bakeup)
		{
			p_move_time = 0;
			move_begin = 4;
			SHUTTER_OFF;
			p_amount+=1;
			mcu_set_data_to_apk(0x09, p_amount);
		}
	}
	else if(move_begin == 4)
	{
		if(p_move_time >= stop_time_bakeup)
		{
			if(p_amount >= glob_para.shoot_amount)
			{
				move_begin = 0;
				motor_stop();
				delay_start= 0;
				delay_send_pake(1);
				LL_USART_EnableIT_TXE(USART1);
				return;
			}
			else
			{
				p_move_time = 0;
				
				if(glob_para.stop_shoot)
				{
					move_begin = 2;
				}
				else
				{
					move_begin = 1;
					motor_start();
				}
			}
		}
	}
}
*/
void delay_mode_init(void)
{
//	frames_time = glob_para.sl_f_time_s + glob_para.sl_f_time_m*60 + glob_para.sl_f_time_h*60*60;
//	fast_time_bakeup = glob_para.sl_sh_time_s + glob_para.sl_sh_time_m*60 + glob_para.sl_sh_time_h*60*60;
}


/////////////////////////////////////////// end /////////////////////////////////

/// for auto_mode stop 1s when reach the a point or b point
void auto_stop_1S(void)
{
	if(stop_1S==0)return;

	if((page_id == AUTO_MODE) || (page_id == DELAY_SET) || (page_id == DELAY_DIS))
	{
		if(boot_time == 0)
		{
			if(stop_1S == 0x01) // reach a point
			{
				set_1S(0);
				glob_para.orbital_dir=A_TO_B;
				motor_direction_change();
				auto_return_acstion();
			}
			else if(stop_1S == 0x02) // reach b point
			{
				set_1S(0);
				
				glob_para.orbital_dir=B_TO_A;
				motor_direction_change();
				auto_return_acstion();
			}
			else if(stop_1S == 0x03) // move to a point and stop 1S
			{
				set_1S(0);
				fisrt_time_begin();
			}
		}
	}
}

//////////////////////////////// yun tai /////////////////////////////////////////////





